package com.peng.crm.workbench.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peng.crm.settings.dao.DicValueDao;
import com.peng.crm.settings.dao.UserDao;
import com.peng.crm.workbench.dao.ClueDao;
import com.peng.crm.workbench.entity.Clue;
import com.peng.crm.workbench.service.ClueService;
import com.peng.crm.workbench.util.IDSelNameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线索表(Clue)表服务实现类
 *
 * @author makejava
 * @since 2022-07-02 19:11:34
 */
@Service("clueService")
public class ClueServiceImpl implements ClueService {
    @Resource
    private ClueDao clueDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private DicValueDao dicValueDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Clue queryById(String id) {
        return clueDao.queryById(id);
    }

    /**
     * 通过ID查询单条数据，找到其中关联的ID对应的值  字典表与用户表
     * @param id
     * @return
     */
    public Clue selectById(String id){
        Clue clue = clueDao.queryById(id);
        /*if (clue.getState() != null && !"".equals(clue.getState())){
            DicValue dicValue = dicValueDao.selectById(clue.getState());
            clue.setState(dicValue.getText());
        }
        if (clue.getSource() != null && !"".equals(clue.getSource())){
            DicValue dicValue = dicValueDao.selectById(clue.getSource());
            clue.setSource(dicValue.getText());
        }
        if (clue.getEditBy() != null && !"".equals(clue.getEditBy())){
            User user = userDao.selectById(clue.getEditBy());
            clue.setEditBy(user.getName());
        }
        if (clue.getCreateBy() != null && !"".equals(clue.getCreateBy())){
            User user = userDao.selectById(clue.getCreateBy());
            clue.setCreateBy(user.getName());
        }*/
        IDSelNameUtil idSelNameUtil = new IDSelNameUtil();
        idSelNameUtil.setUserDao(userDao);
        idSelNameUtil.setDicValueDao(dicValueDao);
        clue.setState(idSelNameUtil.getDicText(clue.getState()));
        clue.setSource(idSelNameUtil.getDicText(clue.getSource()));
        clue.setEditBy(idSelNameUtil.getUserName(clue.getEditBy()));
        clue.setCreateBy(idSelNameUtil.getUserName(clue.getCreateBy()));
        return clue;
    }

    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<Clue> list() {
        return clueDao.list();
    }

    /**
     * 分页查询
     *
     * @param pageNumber    页码
     * @param pageSize      条数
     * @return 查询结果
     */
    @Override
    public Map<String, Object> listPage(Integer pageNumber, Integer pageSize, Clue clue) {
        PageHelper.startPage(pageNumber,pageSize);
        PageInfo<Clue> pageInfo = new PageInfo(clueDao.select(clue));
        List<Clue> pagelist = pageInfo.getList();
        Map<String, Object> pageMap = new HashMap<>();
        IDSelNameUtil idSelNameUtil = new IDSelNameUtil();
        idSelNameUtil.setUserDao(userDao);
        idSelNameUtil.setDicValueDao(dicValueDao);
        for (Clue clue1 : pagelist) {
            clue1.setSource(idSelNameUtil.getDicText(clue1.getSource()));
            clue1.setState(idSelNameUtil.getDicText(clue1.getState()));
        }
        pageMap.put("list",pagelist);
        pageMap.put("total",pageInfo.getTotal());
        return pageMap;
    }

    /**
     * 取到线索状态的值与线索来源的值
     */
    /*public Map<String,Object> getClue(List<Clue> clueList){
        HashMap<String, Object> map = new HashMap<>();
        if (clueList != null){
            for (Clue clue1 : clueList) {
                if (clue1.getSource() != null && !("".equals(clue1.getSource()))){
                    DicValue dicValue = dicValueDao.selectById(clue1.getSource());
                    clue1.setSource(dicValue.getText());
                }
                if (clue1.getState() != null && !"".equals(clue1.getState() )){
                    DicValue dicValue = dicValueDao.selectById(clue1.getState());
                    clue1.setState(dicValue.getText());
                }
            }
            map.put("list",clueList);
        }
        return map;
    }*/

    /**
     * 新增数据
     *
     * @param clue 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(Clue clue) {
        return clueDao.insert(clue)>0;
    }

    /**
     * 修改数据
     *
     * @param clue 实例对象
     * @return 实例对象
     */
    @Override
    public Clue update(Clue clue) {
        this.clueDao.update(clue);
        return this.queryById(clue.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.clueDao.deleteById(id) > 0;
    }

    /**
     * 删除多条数据
     * @param ids   主键数组
     * @return
     */
    @Override
    public boolean removeBatch(String[] ids) {
        return clueDao.deletes(ids);
    }
}
